package com.maiworld.seller.controller;

import java.io.IOException;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.maiworld.content.service.SubjectLC2Service;
import com.maiworld.pojo.Subject;

import entity.PageResult;
import entity.Result;

@RestController
@RequestMapping("/subjectLC2")
public class SubjectLC2Controller {
	@Reference
	private SubjectLC2Service subjectLC2Service;
	
	/**
	 * 修改
	 * @param question
	 * @return
	 */
	@RequestMapping(value = "/savSubject",method =RequestMethod.POST)
	public Result saveArticle(@RequestBody Subject question) {
		Result success = new Result(false,"添加失败");		 
		//System.out.println(question);
		int row = subjectLC2Service.saveSujectLClC2(question);
		if(row > 0) {
			success.setSuccess(true);
			success.setMessage("添加成功");
		}else {
			
		}
		return success;
	}
	
	/**
	 * 查询 分页
	 * @param question
	 * @param page
	 * @param rows
	 * @return
	 */
	@RequestMapping(value = "/search",method =RequestMethod.POST)
	public PageResult search(@RequestBody Subject question,@RequestParam("page") int page,@RequestParam("rows") int rows) {
		/*System.out.println(question);*/
		PageResult res = subjectLC2Service.selectBySubjectC2(question, page, rows);
		return res;
	}
	
	/**
	 * 重置科目四 题库
	 * @return
	 */
	@RequestMapping(value = "/resetC2",method =RequestMethod.GET)
	public Result resetSubjectLC2() {
		Result success = new Result(false,"重置失败");

		List<Subject> subjects = httpToJuhe("http://v.juhe.cn/jztk/query?subject=4&model=&testType=order&=&key=9528700d7939cc240cfe95eb72c8c7d6");
		
		int row = subjectLC2Service.resetSubjectLC2(subjects);
		if(row > 0) {
			success.setSuccess(true);
			success.setMessage("重置成功");
		}else {
			
		}
		return success;
	}
	/**
	 * 请求聚合数据 
	 * @return 
	 */
	public List<Subject> httpToJuhe(String url) {
        // 创建 HttpClient 客户端
        CloseableHttpClient httpClient = HttpClients.createDefault();

        // 创建 HttpGet 请求
        HttpGet httpGet = new HttpGet(url);
        // 设置长连接
        httpGet.setHeader("Connection", "keep-alive");
        // 设置代理（模拟浏览器版本）
        httpGet.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36");
        // 设置 Cookie
        httpGet.setHeader("Cookie", "UM_distinctid=16442706a09352-0376059833914f-3c604504-1fa400-16442706a0b345; CNZZDATA1262458286=1603637673-1530123020-%7C1530123020; JSESSIONID=805587506F1594AE02DC45845A7216A4");

        CloseableHttpResponse httpResponse = null;
        HttpEntity httpEntity = null;
        String httpResStr = null;
        try {
            // 请求并获得响应结果
            httpResponse = httpClient.execute(httpGet);
            httpEntity = httpResponse.getEntity();
         // 输出请求结果
//         System.out.println(EntityUtils.toString(httpEntity));
         httpResStr = EntityUtils.toString(httpEntity);
            
        } catch (IOException e) {
            e.printStackTrace();
        }          
        // 无论如何必须关闭连接
        finally {
            if (httpResponse != null) {
                try {
                    httpResponse.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            if (httpClient != null) {
                try {
                    httpClient.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
		//将传递过来的json串转化为JSONObject
		JSONObject jsonObject = JSONObject.parseObject(httpResStr);
		JSONArray jsonArray = (JSONArray)jsonObject.get("result");
		List<Subject> subjects = (List<Subject>)JSONArray.parseArray(jsonArray.toString(), Subject.class);

		return subjects;
	}
}
